package com.sansecy.customview.activity;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.SeekBar;

public class HorizontalScaleProgressBar extends SeekBar {

    private int mW;
    private int mH;
    private Paint mPaint;
    private int mItemSpace;
    private int mDividerHeight = 48;
    private int mDividerWidth = 6;
    private int mCurrentScale = 0;
    private int mScaleCount = 10;
    private int mSelectColor = Color.WHITE;
    private int mUnSelectColor = Color.GRAY;


    public HorizontalScaleProgressBar(Context context) {
        super(context);
    }

    public HorizontalScaleProgressBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setColor(mUnSelectColor);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(mDividerWidth);
        mPaint.setTextAlign(Paint.Align.CENTER);
        mPaint.setTextSize(45);
    }

    public int getScaleCount() {
        return mScaleCount;
    }

    public void setScaleCount(int scaleCount) {
        mScaleCount = scaleCount;
        requestLayout();
    }

    public void setDividerHeight(int dividerHeight) {
        mDividerHeight = dividerHeight;
        requestLayout();
    }

    public void setDividerWidth(int dividerWidth) {
        mDividerWidth = dividerWidth;
        requestLayout();
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        for (int i = 1; i <= mScaleCount; i++) {
            if (mItemSpace * i < mW * ((float)getProgress() / (float)getMax())) {
                mPaint.setColor(mSelectColor);
            } else {
                mPaint.setColor(mUnSelectColor);
            }
            canvas.drawText(String.valueOf(i), mItemSpace * i - mItemSpace / 2f, mH - mDividerHeight, mPaint);
            canvas.drawLine(mItemSpace * i, mH - mDividerHeight, mItemSpace * i, mH, mPaint);
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mH = getMeasuredHeight();
        mW = getMeasuredWidth();
        mItemSpace = getMeasuredWidth() / mScaleCount;
        handleScale();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        boolean touchEvent = super.dispatchTouchEvent(event);
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_MOVE:
                handleScale();
                break;
        }
        return touchEvent;
    }

    private void handleScale() {
        int scale = (int) (mW * ((float) getProgress() / (float) getMax()) / mItemSpace);
        scale = scale + 1;
        if (mCurrentScale != scale) {
            mCurrentScale = scale;
            dispatchScaleChange();
        }
    }

    private void dispatchScaleChange() {
        if (mOnScaleChangeListener != null) {
            mOnScaleChangeListener.onScaleChange(mCurrentScale);
        }
    }

    private OnScaleChangeListener mOnScaleChangeListener;

    public void setOnScaleChangeListener(OnScaleChangeListener onScaleChangeListener) {
        mOnScaleChangeListener = onScaleChangeListener;
    }

    interface OnScaleChangeListener {
        void onScaleChange(int scale);
    }

}
